package the.workshop.bu.common.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import the.workshop.bu.constant.CustomerCache;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.service.ICustomerService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

//@Component
@Slf4j
public class CustomerRunnerComponent implements CommandLineRunner {

    @Resource
    private ICustomerService customerService;

    @Override
    public void run(String... args) throws Exception {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Customer::getIsDeleted, 0);
        log.info(LocalDateTime.now().toString());
        List<Customer> customerList = customerService.list(queryWrapper);
        if (CollectionUtils.isEmpty(customerList)) {
            return;
        }

        for (Customer customer : customerList) {
            CustomerCache.saveName(customer.getName(), customer.getId());
        }
        log.info(LocalDateTime.now().toString());
    }
}
